﻿@namespace ThingsGateway.Components
@using Masa.Blazor

<MListGroup NoAction PrependIcon=@Item.Icon Group=_group SubGroup="SubGroup">
     <ActivatorContent>
         <AppListItemContent Item="Item" />
     </ActivatorContent>

     <ChildContent>
         @foreach (var child in Item?.Children ?? new())
        {
            if (child.Heading is not null)
            {
                <MSubheader Class="text--primary font-weight-black text-uppercase" Inset>@child.Heading</MSubheader>
            }
            else if (child.Divider)
            {
                <MDivider Inset Class="mt-3 mb-2 ml-16"></MDivider>
            }
            else if (child.HasChildren())
            {
                <AppListGroup Item="@child" SubGroup></AppListGroup>
            }
            else
            {
                <AppListItem Item="child"></AppListItem>
            }
        }
    </ChildContent>
</MListGroup>


@code {
    /// <summary>
    /// icon
    /// </summary>
    [Parameter]
    public string Icon { get; set; }

    /// <summary>
    /// item
    /// </summary>
    [Parameter, EditorRequired]
    public NavItem Item { get; set; }

    /// <summary>
    /// sub
    /// </summary>
    [Parameter]
    public bool SubGroup { get; set; }

    private List<string> _group = new();

    /// <inheritdoc/>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        _group = GenGroup(Item.Children);
    }

    private List<string> GenGroup(List<NavItem> items)
    {
        if (items == null || !items.Any())
            return new List<string>();
        var groups = new List<string>();
        foreach (var item in items)
        {
            groups.AddRange(GenGroup(item.Children));
            if (item.HasChildren())
                continue;
            groups.Add(item.Href);
        }

        return groups;
    }
}